﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace Eugenhome.Atrium
{
    /// <summary>
    /// 
    /// </summary>
    public class PageContent
    {
        /// <summary></summary>
        public List<string> Errors = new List<string>();

        /// <summary></summary>
        public bool Loaded { get; set; }

        /// <summary></summary>
        public bool Responsed { get; set; }

        /// <summary></summary>
        private string _url = "";

        /// <summary></summary>
        private string _proxyUrl = "";

        /// <summary></summary>
        private string _content;

        /// <summary></summary>
        public string Content
        {
            get
            {
                if (_content == null)
                {
                    _content = GetContent();
                }
                return _content;
            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        public PageContent(string url) : this(url, "") { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="proxyUrl"></param>
        public PageContent(string url, string proxyUrl)
        {
            _url = url;
            _proxyUrl = proxyUrl;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetContent()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_url);

            if (!_proxyUrl.Trim().Equals(string.Empty))
            {
                req.Proxy = new WebProxy(_proxyUrl);
            }

            string content = "";

            try
            {
                //req.Timeout = 300; 
                //req.ReadWriteTimeout = 300;
                //req.KeepAlive = false;

                HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                Responsed = true;

                StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding(res.CharacterSet));

                content = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception ex)
            {
                Errors.Add("HttpWebResponse: " + ex.Message);
                Loaded = false;
            }
            return content;
        }






        /// <summary>
        /// Remove HTML from string with Regex.
        /// </summary>
        public static string StripTagsRegex(string source)
        {
            return Regex.Replace(source, "<.*?>", string.Empty);
        }

        /// <summary>
        /// Compiled regular expression for performance.
        /// </summary>
        static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);

        /// <summary>
        /// Remove HTML from string with compiled Regex.
        /// </summary>
        public static string StripTagsRegexCompiled(string source)
        {
            return _htmlRegex.Replace(source, string.Empty);
        }

        /// <summary>
        /// Remove HTML tags from string using char array.
        /// </summary>
        public static string StripTagsCharArray(string source)
        {
            char[] array = new char[source.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < source.Length; i++)
            {
                char let = source[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }

    }
}
